﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Common;
using System.Data;
using V5_Common.FrameWork;

namespace V5_Data {
    public class V5HelpHelper : IV5Helper {
        private static readonly V5HelpHelper m_Instance = new V5HelpHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5HelpHelper Instance {
            get { return m_Instance; }
        }

        #region 变量
        private string _Pager = string.Empty;
        /// <summary>
        /// 分页数据
        /// </summary>
        public string Pager {
            get { return _Pager; }
            set { _Pager = value; }
        }

        private string _PrevUrl = string.Empty;
        /// <summary>
        /// 上一个地址
        /// </summary>
        public string PrevUrl {
            get {
                if (string.IsNullOrEmpty(_PrevUrl)) {
                    _PrevUrl = "javascript:void(0);";
                }
                return _PrevUrl;
            }
            set { _PrevUrl = value; }
        }
        private string _PrevTitle = string.Empty;
        /// <summary>
        /// 上一个标题
        /// </summary>
        public string PrevTitle {
            get {
                if (string.IsNullOrEmpty(_PrevTitle)) {
                    _PrevTitle = "没有了";
                }
                return _PrevTitle;
            }
            set { _PrevTitle = value; }
        }
        private string _NextUrl = string.Empty;
        /// <summary>
        /// 下一个地址
        /// </summary>
        public string NextUrl {
            get {
                if (string.IsNullOrEmpty(_NextUrl)) {
                    _NextUrl = "javascript:void(0);";
                }
                return _NextUrl;
            }
            set { _NextUrl = value; }
        }
        private string _NextTitle = string.Empty;
        /// <summary>
        /// 下一个标题
        /// </summary>
        public string NextTitle {
            get {
                if (string.IsNullOrEmpty(_NextTitle)) {
                    _NextTitle = "没有了";
                }
                return _NextTitle;
            }
            set { _NextTitle = value; }
        }
        private int _PageSize = 20;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize {
            get { return _PageSize; }
            set { _PageSize = value; }
        }

        private string _ChannelType = "Help";
        /// <summary>
        /// 频道类型
        /// </summary>
        public string ChannelType {
            get { return _ChannelType; }
        }
        #endregion

        #region 模板
        private string _TemplateIndex = "Help/Index";
        /// <summary>
        /// 首页模板
        /// </summary>
        public string TemplateIndex {
            get { return _TemplateIndex; }
        }
        private string _TemplateList = "Help/List";
        /// <summary>
        /// 列表模板
        /// </summary>
        public string TemplateList {
            get { return _TemplateList; }
        }
        private string _TemplateView = "Help/View";
        /// <summary>
        /// 内容模板
        /// </summary>
        public string TemplateView {
            get { return _TemplateView; }
        }
        private string _TemplateSearch = "Help/Search";
        /// <summary>
        /// 搜索列表模板
        /// </summary>
        public string TemplateSearch {
            get { return _TemplateSearch; }
        }
        #endregion

        #region
        public DataTable Query_By(string what, string where, string orderby, int limit) {
            DataTable dt = V5.SQL.Query_By(what, "(Select B.ClassTitle,A.* From Tbl_Help A Left Join Tbl_HelpClass B On A.ClassId=B.ClassId) AA", where, orderby, limit);
            dt.Columns.Add(new DataColumn("Url"));
            dt.Columns.Add(new DataColumn("UrlList"));
            foreach (DataRow item in dt.Rows) {
                item["Url"] = this.UrlView(new { ID = item["ID"].ToString(), PageID = "1" });
                item["UrlList"] = this.UrlList(new { ClassID = item["ClassID"].ToString(), PageID = "1" });
            }
            return dt;
        }

        public DataTable Query_By_List(string what, string where, string orderby, int limit) {
            DataTable dt = V5.SQL.Query_By(what, "Tbl_HelpClass", where, orderby, limit);
            dt.Columns.Add(new DataColumn("Url"));
            foreach (DataRow item in dt.Rows) {
                item["Url"] = this.UrlList(new { ClassID = item["ClassID"].ToString(), PageID = "1" });
            }
            return dt;
        }

        public DataTable Query_By_Pager(string what, string where, string orderby, int limit, int pageIndex, string UrlList) {
            throw new NotImplementedException();
        }

        public DataRow Get_By(string ID) {
            this.PrevUrl = string.Empty;
            this.PrevTitle = string.Empty;

            this.NextUrl = string.Empty;
            this.NextTitle = string.Empty;
            using (DataTable dt = V5.SQL.Query_By("*", "(Select B.ClassTitle,A.* From  Tbl_Help A Left Join Tbl_HelpClass B On A.ClassId=B.ClassId) AA", " ID=" + ID, "", 1)) {
                if (dt != null && dt.Rows.Count > 0) {
                    DataRow dr = dt.Rows[0];
                    dt.Columns.Add(new DataColumn("Url"));
                    dt.Columns.Add(new DataColumn("UrlList"));
                    dt.Rows[0]["Url"] = this.UrlView(new { ID = ID, PageID = "1" });
                    dt.Rows[0]["UrlList"] = this.UrlList(new { ClassID = dr["ClassID"].ToString(), PageID = "1" });
                    //上一个 下一个
                    DataTable dtPrev = V5.SQL.Query_By("*", "Tbl_Help", " ID=(Select Max(ID) From Tbl_Help Where ID<" + ID + ")", "", 1);
                    if (dtPrev != null && dtPrev.Rows.Count == 1) {
                        this.PrevUrl = this.UrlView(new { ID = dtPrev.Rows[0]["ID"].ToString(), PageID = 1 });
                        this.PrevTitle = dtPrev.Rows[0]["Title"].ToString();
                    }
                    DataTable dtNext = V5.SQL.Query_By("*", "Tbl_Help", " ID=(Select Min(ID) From Tbl_Help Where ID>" + ID + ")", "", 1);
                    if (dtNext != null && dtNext.Rows.Count == 1) {
                        this.NextUrl = this.UrlView(new { ID = dtNext.Rows[0]["ID"].ToString(), PageID = 1 });
                        this.NextTitle = dtNext.Rows[0]["Title"].ToString();
                    }
                    return dt.Rows[0];
                }
                return dt.NewRow();
            }
        }

        public DataRow Get_By_List(string ClassID) {
            throw new NotImplementedException();
        }

        public string UrlIndex() {
            return V5.Url.Action(this.TemplateIndex, null);
        }

        public string UrlIndex(int showType) {
            return V5.Url.Action(this.TemplateIndex, null, showType);
        }

        public string UrlView(object attributes) {
            return V5.Url.Action(this.TemplateView, attributes);
        }

        public string UrlList(object attributes) {
            return V5.Url.Action(this.TemplateList, attributes);
        }

        public string UrlView(object attributes, int showType) {
            return V5.Url.Action(this.TemplateView, attributes, showType);
        }

        public string UrlList(object attributes, int showType) {
            return V5.Url.Action(this.TemplateList, attributes, showType);
        }
        public string UrlSearch(object attributes) {
            return V5.Url.Action(this.TemplateList, attributes);
        }

        public string UrlSearch(object attributes, int showType) {
            return V5.Url.Action(this.TemplateList, attributes, showType);
        }
        #endregion



    }
}